limsPurchase.auditPage = function (pageType) {
    return {
        getGridParam: function () {
            var _this = this;
            return {
                type: 'grid',
                id: 'lims-purchase-audit-list-grid' + pageType,
                url: limsPurchase.baseUrl + '/queries/tasks',
                service: 'limsPurchaseServiceImpl',
                dbTable: 'T_LIMS_PURCHASE',
                columns: limsPurchase.getShowListColumns(),
                requestData: {
                    bpmn_statusCode: _this.param.pageType
                },
                onLoadSuccess: function (data) {
                    if (!Gikam.isEmpty(data)) {
                        this.activeRowByIndex(0);
                    } else {
                        // _this.refreshTabs(null)
                    }
                },
                onRowActive: function (index, row) {
                    // _this.refreshTabs(row)
                },
                onCellClick: function (field, row) {
                    field === 'applyCode' && _this.loadDetailPage(row)
                },
                page: true,
                toolbar: [{
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                    icon: 'pass',
                    onClick: function () {
                        var grid = Gikam.getComp('lims-purchase-audit-list-grid' + pageType);
                        var selection = grid.getSelections()
                        Gikam.create('workflow').pass({
                            data: grid.getSelections(),
                            pageObject: limsPurchase,
                            formatterToolbar: (toolbar, type, formId) => {
                                toolbar.forEach(b => {
                                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                                        b.class = 'blue'
                                        b.icon = 'save'
                                        b.color = 'white'
                                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                                        b.icon = 'back'
                                    }
                                })
                                return toolbar
                            }
                        }).done(function () {
                            grid.refresh();
                        });
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                    icon: 'reject',
                    onClick: function () {
                        var grid = Gikam.getComp('lims-purchase-audit-list-grid' + pageType);
                        Gikam.create('workflow').reject({
                            data: grid.getSelections(),
                            pageObject: limsPurchase,
                            formatterToolbar: (toolbar, type, formId) => {
                                toolbar.forEach(b => {
                                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                                        b.class = 'blue'
                                        b.icon = 'save'
                                        b.color = 'white'
                                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                                        b.icon = 'back'
                                    }
                                })
                                return toolbar
                            }
                        }).done(function () {
                            grid.refresh();
                        });
                    }
                }]
            }
        },

        loadDetailPage: function (row) {
            workspace.window.load(limsPurchase.baseUrl + '/page/detail' + Gikam.param({
                id: row.id,
                step: 'audit'
            }));
        },

        refreshTabs: function (row) {
            let _this = this
            Gikam.getAsyncComp(_this.idManager.auditRecordGrid).done(function (comp) {
                if (row && row.id) {
                    comp.refresh({
                        url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_PURCHASE&id=' + row.id
                    })
                } else {
                    comp.refresh({
                        url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_PURCHASE&id=' + 0
                    })
                }
            })
            Gikam.getAsyncComp('lims-purchase-audit-list-material-grid' + pageType).done(function (grid) {
                if (row) {
                    grid.refresh({
                        url: Gikam.printf(limsPurchase.baseUrl + '/{id}/materials/queries', {
                            id: row.id
                        })
                    });
                } else {
                    grid.refresh({
                        url: Gikam.printf(limsPurchase.baseUrl + '/{id}/materials/queries', {
                            id: -1
                        })
                    });
                }
            });
            _this.refreshAttachments(_this.idManager.trainRecordGrid)
        },

        getAttachUploaderId: function (gridId) {
            return gridId + '-uploader'
        },

        refreshAttachments: function (gridId) {
            let _this = this
            let grid = Gikam.getComp(gridId)
            let actRow = grid.getActivedRow()
            Gikam.getAsyncComp(_this.getAttachUploaderId(gridId)).done(uploader => {
                if (actRow && actRow.id) {
                    uploader.setOptions({
                        bizId: actRow.id,
                    })
                } else {
                    uploader.setOptions({
                        bizId: -1,
                    })
                }
            })
        },

        getAttachmantStruct: function (gridId, dbTable, readonly) {
            let _this = this
            return {
                type: 'uploader',
                immediateUpload: true,
                id: _this.getAttachUploaderId(gridId),
                dbTable: dbTable,
                filter: false,
                genericQuery: false,
                readonly: readonly,
                onBeforeUpload: function () {
                    var activedRow = Gikam.getComp(gridId).getActivedRow()
                    if (Gikam.isEmpty(activedRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                        return false
                    }
                },
            }
        },

        getAuditRecordGrid: function () {
            let _this = this
            return {
                type: 'grid',
                id: _this.idManager.auditRecordGrid,
                columns: limsPurchase.getFlowListColumns()
            }
        },

        getMaterialGrid: function () {
            var _this = this;
            return {
                type: 'grid',
                id: 'lims-purchase-audit-list-material-grid' + pageType,
                service: 'limsPurchaseMaterialServiceImpl',
                dbTable: 'T_LIMS_PURCHASE_MATERIAL',
                deleteFormatter: function (row) {
                    return row.id;
                },
                columns: limsPurchase.getMaterialShowListColumns()
            };
        },

        create: function () {
            var _this = this;
            Gikam.create('layout', {
                id: 'lims-purchase-audit-list-layout' + pageType,
                renderTo: workspace.window.$dom,
                center: {
                    items: [this.getGridParam()]
                }
            });
        },

        idManager: {
            trainRecordGrid: 'lims-purchase-audit-list-grid' + pageType,
            auditRecordGrid: 'lims-purchase-audit-list-grid' + pageType + '-record',
        },

        init: function (param) {
            this.param = param;
            this.create();
        }
    }
};
